package appapi.controller.oider;

import appapi.Global.BusinessException;
import appapi.Request.EquiPageReq;
import appapi.Request.EquipOperPageReq;
import appapi.Request.EquiPageReq;
import appapi.Request.InspectionPageReq;
import appapi.Request.SelfcheckPageReq;
import appapi.Request.record.MonthRecPageReq;
import appapi.Vo.*;
import appapi.Vo.AppVo.AppRectVo;
import appapi.Vo.AppVo.GetEquipList;
import appapi.Vo.record.GetAlarmPageVo;
import appapi.common.BusinessTypeEnum;
import appapi.common.ResponseBean;
import appapi.config.CurrentUser;
import appapi.config.MyLog;
import appapi.dto.AddEquipMaintenanDto;
import appapi.dto.GetAlarmPageDto;
import appapi.dto.MonthRecModel;
import appapi.dto.UpdateRectifDto;
import appapi.dto.*;
import appapi.entity.Zhongjiao.*;
import appapi.entity.app.RectificationEntity;
import appapi.entity.basic.UserInfo;
import appapi.entity.record.MonthInspectionEntity;
import appapi.model.ResponseDataModel;
import appapi.model.ResponseModel;
import appapi.service.*;
import appapi.utils.ExcelUtils;
import appapi.service.impl.AlarmledgerServiceImpl;
import appapi.utils.ExcelUtils;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author djq
 * @date 2023/5/17 15:36
 * @description:
 */

@Api(tags = "设备记录")
@RestController
@RequestMapping(value = "/api/deviceRecord")
public class DeviceRecordController {

    @Resource
    private IAnnexService iAnnexService;
    @Resource
    private IRectificationService iRectification;
    @Resource
    private IEquipmentMaintenanService iEquipmentMaintenanService;
    @Resource
    IAlarmledgerService iAlarmledgerService;
    @Resource
    IRectificationService iRectificationService;

    //region 附件

    /**
     * 上传附件
     *
     * @return
     * @auther djq 2023/5/19
     */
    @ApiOperation(value = "上传附件", notes = "上传附件")
    @PostMapping(value = "/AddDismant")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "formData", name = "dto", dataType = "object", value = "附件参数")
    })
    public ResponseBean AddDismant(@Validated @RequestBody AnnexDto dto) {
        try {
            iAnnexService.InsertAnnex(dto);
            return ResponseBean.success();
        } catch (BusinessException exception) {

            return ResponseBean.fail("上传附件异常: ".concat(exception.getMessage()));
        }
    }

    /**
     * 删除附件
     *
     * @return
     * @auther djq 2023/5/19
     */
    @ApiOperation(value = "删除附件", notes = "删除附件")
    @PostMapping(value = "/DelDismant")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "annexId", dataType = "Long", value = "附件id")
    })
    public ResponseModel DelDismant(Long annexId) {
        ResponseDataModel responseModel = new ResponseDataModel();
        try {
            iAnnexService.DelAnnex(annexId);

        } catch (BusinessException exception) {

            responseModel.setMessage("删除附件异常: ".concat(exception.getMessage()));
            responseModel.setCode(exception.getCode());
        }
        return responseModel;
    }
    //endregion

    //region 设备维保

    /**
     * 新增一条维保
     *
     * @return
     * @auther djq 2023/5/25
     */
    @ApiOperation(value = "维保-新增一条维保", notes = "维保-新增一条维保")
    @PostMapping(value = "/AddEquipMaintenan")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "formData", name = "dto", dataType = "object", value = "设备参数")
    })
    @MyLog(title = "新增一条维保",businessType = BusinessTypeEnum.insert)
    public ResponseBean AddEquipMaintenan(@CurrentUser UserInfo userInfo,@Validated @RequestBody AddEquipMaintenanDto dto) {
        try {
            dto.setCreateUser(userInfo.getUsername());
            dto.setMainSource(userInfo.getUserid().intValue());
            iEquipmentMaintenanService.InsertMaintenan(dto);
            return ResponseBean.success();
        } catch (BusinessException exception) {
            return ResponseBean.fail("新增维保异常: ".concat(exception.getMessage()));
        }
    }
    /**
     * 查看整改单详情
     */
    @ApiOperation(value = "查看整改单详情", notes = "查看整改单详情")
    @GetMapping(value = "/getRectficaDetails")
    public ResponseBean<AppRectVo> getRectficaDetail(GetRectfiDetailDto dto) {
        try {

            AppRectVo list = iRectificationService.selectRectDetail(dto);

            return ResponseBean.success(list);
        } catch (BusinessException exception) {
            return ResponseBean.fail("查看整改单详情异常: ".concat(exception.getMessage()));
        }
    }
    /**
     * 修改一条维保
     *
     * @return
     * @auther djq 2023/5/25
     */
    @ApiOperation(value = "维保-修改一条维保", notes = "维保-修改一条维保")
    @PostMapping(value = "/UpdateEquipMaintenan")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "formData", name = "dto", dataType = "object", value = "设备参数")
    })
    @MyLog(title = "修改一条维保",businessType = BusinessTypeEnum.update)
    public ResponseBean UpdateEquipMaintenan(@Validated @RequestBody EquipmentmaintenanDto dto) {
        try {
            iEquipmentMaintenanService.UpdateMaintenan(dto);
            return ResponseBean.success();
        } catch (BusinessException exception) {
            return ResponseBean.fail("新增维保异常: ".concat(exception.getMessage()));
        }
    }


    /**
     * 分页获取设备维保记录
     *
     * @return
     * @author djq 2023/5/17
     */
    @ApiOperation(value = "维保-分页获取设备维保记录", notes = "维保-分页获取设备维保记录")
    @GetMapping(value = "/GetMaintenanListByPage")
    public ResponseBean<MaintenanPageVo> GetMaintenanListByPage(@CurrentUser UserInfo userInfo, InspectionPageReq req) {
        try {
            if (req.getCurrPage() == null || req.getPageSize() == null) {
                req.setCurrPage(1);
                req.setPageSize(10);
            }
            List<MaintenanPageVo> projects = iEquipmentMaintenanService.SelectMaintenanPage(userInfo,req);

            PageInfo pageInfo = new PageInfo(projects, req.getPageSize());

            return ResponseBean.success(pageInfo);
        } catch (BusinessException exception) {
            return ResponseBean.fail("分页获取设备维保记录异常: ".concat(exception.getMessage()));
        }
    }

    /**
     * 维保记录导出
     *
     * @return
     * @author lc 2023/06/13
     */
    @ApiOperation(value = "维保记录导出", notes = "维保记录导出接口", produces = "application/octet-stream")
    @GetMapping(value = "/MaintenanceRecExport")
    @ApiOperationSupport(ignoreParameters = {"currPage", "pageSize"})
    @MyLog(title = "维保记录导出",businessType = BusinessTypeEnum.export)
    public ResponseBean EquipmentRecExport(InspectionPageReq req, HttpServletResponse response) {
        try {
            List<MaintenanPageVo> list = iEquipmentMaintenanService.getLists(req);
            if (list.size() == 0) {
                return ResponseBean.success("数据源为空");
            } else if (list.size() > 5000) {
                return ResponseBean.fail("记录超过5000，请重新选择查询条件");
            } else {
                ExcelUtils.exportExcel(list, "维保记录", "维保记录sheet", MaintenanPageVo.class, "维保记录", response);
                return ResponseBean.success();
            }
        } catch (Exception e) {
            return ResponseBean.fail("维保记录导出异常");
        }
    }

    /**
     * 获取设备维保台账详情
     *
     * @return
     * @author djq 2023/5/25
     */
    @ApiOperation(value = "维保-获取设备维保台账详情", notes = "维保-获取设备维保台账详情")
    @GetMapping(value = "/GetEquipMaintenanDetail")
    @ApiImplicitParam(paramType = "query", name = "MaintenanId", dataType = "Long", value = "维保记录id")
    public ResponseBean GetEquipMaintenanDetail(Long MaintenanId) {
        try {

            EquipMaintenanVo list = iEquipmentMaintenanService.SelectMaintenanDetail(MaintenanId);

            return ResponseBean.success(list);
        } catch (BusinessException exception) {
            return ResponseBean.fail("获取设备维保台账详情异常: ".concat(exception.getMessage()));
        }
    }
    //endregion

    //region 整改记录

    /**
     * 分页获取设备整改记录
     *
     * @return
     * @author djq 2023/5/22
     */
    @ApiOperation(value = "整改-分页获取设备整改记录", notes = "整改-分页获取设备整改记录")
    @GetMapping(value = "/GetRectificationListByPage")
    public ResponseBean GetRectificationListByPage(@CurrentUser UserInfo userInfo, InspectionPageReq req) {
        try {
            if (req.getCurrPage() == null || req.getPageSize() == null) {
                req.setCurrPage(1);
                req.setPageSize(10);
            }
            List<Map<String, Object>> projects = iRectification.SelectRectificationPage(userInfo, req);
            PageInfo pageInfo = new PageInfo(projects, req.getPageSize());
            return ResponseBean.success(pageInfo);
        } catch (BusinessException exception) {
            return ResponseBean.fail("分页获取设备整改记录异常: ".concat(exception.getMessage()));
        }
    }

    @ApiOperation(value = "整改记录导出", notes = "整改记录导出接口", produces = "application/octet-stream")
    @GetMapping(value = "/RectificationRecExport")
    @ApiOperationSupport(ignoreParameters = {"currPage", "pageSize"})
    @MyLog(title = "整改记录导出",businessType = BusinessTypeEnum.update)
    public ResponseBean RectificationRecExport(InspectionPageReq req, HttpServletResponse response) {
        try {
            List<EquipmentsExportVo> list = iRectification.getList(req);
            if (list.size() == 0) {
                return ResponseBean.success("数据源为空");
            } else if (list.size() > 5000) {
                return ResponseBean.fail("记录超过5000，请重新选择查询条件");
            } else {
                ExcelUtils.exportExcel(list, "整改记录", "整改记录sheet", EquipmentsExportVo.class, "整改记录", response);
                return ResponseBean.success();
            }
        } catch (Exception e) {
            return ResponseBean.fail("整改记录导出异常");
        }
    }

    /**
     * 获取设备整改记录列表
     *
     * @return
     * @author djq 2023/5/22
     */
    @ApiOperation(value = "整改-获取设备整改记录列表", notes = "整改-获取设备整改记录列表")
    @GetMapping(value = "/GetRectificationList")
    @ApiImplicitParam(paramType = "query", name = "equipId", dataType = "Long", value = "设备id")
    public ResponseModel GetRectificationList(Long equipId) {
        ResponseDataModel responseModel = new ResponseDataModel();
        try {

            List<Map<String, Object>> list = iRectification.SelectRectificationList(equipId);

            responseModel.setData(list);
            responseModel.setRcount(list.size());
        } catch (BusinessException exception) {

            responseModel.setMessage("获取设备维保记录列表异常: ".concat(exception.getMessage()));
            responseModel.setCode(exception.getCode());
        }
        return responseModel;
    }

//    /**
//     * 添加整改记录
//     *
//     * @return
//     * @author djq 2023/5/20
//     */
//    @ApiOperation(value = "整改-添加整改记录", notes = "整改-添加整改记录")
//    @PostMapping(value = "/AddRectification")
//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType = "formData", name = "dto", dataType = "object", value = "整改参数")
//    })
//    public ResponseModel AddRectification(@Validated RectificationDto dto) {
//        ResponseDataModel responseModel = new ResponseDataModel();
//        try {
//            iRectification.InsertRectification(dto);
//
//        } catch (BusinessException exception) {
//
//            responseModel.setMessage("添加整改记录异常: ".concat(exception.getMessage()));
//            responseModel.setCode(exception.getCode());
//        }
//        return responseModel;
//    }

    /**
     * 编辑整改记录
     *
     * @return
     * @author djq 2023/5/22
     */
    @ApiOperation(value = "整改-编辑整改记录", notes = "整改-编辑整改记录")
    @PostMapping(value = "/EditRectification")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "formData", name = "dto", dataType = "object", value = "整改参数")
    })
    @MyLog(title = "编辑整改记录",businessType = BusinessTypeEnum.update)
    public ResponseModel EditRectification(UpdateRectifDto dto) {
        ResponseDataModel responseModel = new ResponseDataModel();
        try {
            iRectification.UpdateRectification(dto);

        } catch (BusinessException exception) {

            responseModel.setMessage("编辑整改记录异常: ".concat(exception.getMessage()));
            responseModel.setCode(exception.getCode());
        }
        return responseModel;
    }

    /**
     * 删除整改记录
     *
     * @return
     * @author djq 2023/5/22
     */
    @ApiOperation(value = "整改-删除整改记录", notes = "整改-删除整改记录")
    @PostMapping(value = "/DeleteRectification")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "rectId", dataType = "Long", value = "整改记录id")
    })
    @MyLog(title = "删除整改记录",businessType = BusinessTypeEnum.delete)
    public ResponseModel DeleteRectification(Long rectId) {
        ResponseDataModel responseModel = new ResponseDataModel();
        try {
            iRectification.DelRectification(rectId);
        } catch (BusinessException exception) {

            responseModel.setMessage("删除整改记录异常: ".concat(exception.getMessage()));
            responseModel.setCode(exception.getCode());
        }
        return responseModel;
    }

    //endregion

    //region 设备告警台账

    /**
     * 分页获取设备告警台账
     *
     * @return
     * @author djq 2023/5/22
     */
    @ApiOperation(value = "整改-分页获取设备告警台账", notes = "整改-分页获取设备告警台账")
    @GetMapping(value = "getAlarmPage")
    @ApiImplicitParam(paramType = "formData", name = "dto", dataType = "object", value = "")
    public ResponseBean<PageInfo<GetAlarmPageVo>> getAlarmPage(@CurrentUser UserInfo userInfo, GetAlarmPageDto dto) {
        try {
            if (dto.getCurrPage() == null || dto.getPageSize() == null) {
                dto.setCurrPage(1);
                dto.setPageSize(10);
            }
            PageInfo<GetAlarmPageVo> projects = iAlarmledgerService.getPage(userInfo, dto);
            return ResponseBean.success(projects);
        } catch (BusinessException exception) {
            return ResponseBean.fail("分页获取设备告警台账异常: ".concat(exception.getMessage()));
        }
    }

    /**
     * 告警台账记录导出
     * @return
     */
    @ApiOperation(value = "告警台账记录导出", notes = "告警台账记录导出接口", produces = "application/octet-stream")
    @GetMapping(value = "/AlarmLedgerRecExport")
    @ApiOperationSupport(ignoreParameters = {"currPage", "pageSize"})
    public ResponseBean AlarmLedgerRecExport(GetAlarmPageDto dto, HttpServletResponse response){
        try {
            List<AlarmLedgerModel> list = iAlarmledgerService.getList(dto);
            if (list.size() == 0) {
                return ResponseBean.success("数据源为空");
            } else if (list.size() > 5000) {
                return ResponseBean.fail("记录超过5000，请重新选择查询条件");
            } else {
                ExcelUtils.exportExcel(list, "告警台账记录", "告警台账记录sheet", AlarmLedgerModel.class, "告警台账记录", response);
                return ResponseBean.success();
            }
        } catch (Exception e) {
            return ResponseBean.fail("告警台账记录导出异常");
        }

    }
    //endregion
}
